package cn.net.chenbao.medical;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import org.xutils.x;

import cn.net.chenbao.medical.api.Api;
import cn.net.chenbao.medical.util.Consts;
import cn.net.chenbao.medical.util.SharedPreferenceUtils;
import cn.net.chenbao.medical.util.WWToast;
import cn.net.chenbao.medical.util.ZLog;

/**
 * Created by Bubble on 2017/10/17.
 */

public class MedicalApplication extends Application {
    /**
     * sessionId
     */
    private String sessionId;

    private String operName;

    private String hostAddress;

    /**
     * deviceId
     */
    private String deviceId;

    /**
     * session失效
     */
    private boolean isSessionPast;

    private static MedicalApplication instance;

    @SuppressLint("HardwareIds")
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        //获取当前设备号（deviceId）
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        assert tm != null;
        deviceId = tm.getDeviceId();
        SharedPreferenceUtils.init(this);
        x.Ext.init(this);
        x.Ext.setDebug(true);
        WWToast.init(this);
        ZLog.isDebug = true;
    }

    public static MedicalApplication getInstance() {
        return instance;
    }

    public String getSessionId() {
        if (sessionId == null) {
            sessionId = SharedPreferenceUtils.getInstance().getSessionId();
        }
        return sessionId;
    }

    /**
     * 用于登录用
     */
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
        SharedPreferenceUtils.getInstance().saveSessionId(sessionId);
        isSessionPast = false;// 设置id的时候(登录)sessionPast重置
    }

    /**
     * 暂用于清除
     */
    public void updataSessionId(String sessionId) {
        this.sessionId = sessionId;
        isSessionPast = false;

    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getOperName() {
        if (operName == null) {
            operName = SharedPreferenceUtils.getInstance().getOperName();
        }
        return operName;
    }

    public void setOperName(String operName) {
        this.operName = operName;
        SharedPreferenceUtils.getInstance().saveOperName(operName);
    }

    public String getHostAddress() {
        if (hostAddress == null) {
            hostAddress = SharedPreferenceUtils.getInstance().getHostAddress();
        }
        return hostAddress;
    }

    public void setHostAddress(String address) {
        this.hostAddress = address;
        Api.WW_ONLINE = hostAddress;
        Api.PDA_API = Api.WW_ONLINE + Api.PDA_JSON;
        SharedPreferenceUtils.getInstance().saveHostAddresse(hostAddress);
    }

    /**
     * 判断登录状态
     *
     * @return
     */
    public boolean isLogin() {
        String userId = SharedPreferenceUtils.getInstance().getSessionId();
        return !TextUtils.isEmpty(userId);
    }

    public void dealSessionPast() {
        if (isSessionPast) {
            return;
        } else {
            isSessionPast = true;
            // TODO:弹窗?跳界面

            startActivity(new Intent(this, MainActivity.class).putExtra(
                    Consts.KEY_SESSION_ERROR, true).setFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK));

        }
    }

}
